﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Player))]
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(MovementAbility))]
public class SlidingAbility : MonoBehaviour {

    public float Distance;
    public float Time;

    private Player player;
    private Animator animator;
    private MovementAbility movementAbility;
    private float totalTime;

    public void Start () {
        player = GetComponent<Player>();
        animator = GetComponent<Animator>();
        movementAbility = GetComponent<MovementAbility>();
    }
	
	public void Update () {
        if(player.CurrentSate != PlayerState.Slid)
        {
            animator.SetBool("isSlide", false);
            if (Input.GetKeyDown(KeyCode.S))
            {
                totalTime = 0;
                player.CurrentSate = PlayerState.Slid;
                animator.SetBool("isSlide", true);
            }
        }
    }

    public void FixedUpdate()
    {
        if(player.CurrentSate == PlayerState.Slid)
        {
            var dx = Distance / Time;
            movementAbility.ToMovement(new Vector2(dx * UnityEngine.Time.deltaTime * (player.Drection ? -1 : 1), 0),false);
            totalTime += UnityEngine.Time.deltaTime;
            if(totalTime > Time)
            {
                player.CurrentSate = PlayerState.Idle;
                animator.SetBool("isSlide", false);
            }
        }
    }
}
